package controllers

import (
	"encoding/json"
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	"strconv"
	"time"
	"user-man/cache"
	"user-man/models"
	"user-man/pkg"
)

type UserController struct {
	beego.Controller
}
type Res struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *UserController) Login() {
	fmt.Println("Login")
	//c.Data["json"] = map[string]interface{}{
	//	"code": 200,
	//	"msg":  "登录成功",
	//}
	//c.ServeJSON()
	//return

	username := c.GetString("username")
	password := c.GetString("password")
	if username == "" || password == "" {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "用户名或者密码为空",
		}
		c.ServeJSON()
		return
	}
	loginErrorRedisKey := "login_error" + username
	errCount, _ := cache.Rdb.Get(cache.Ctx, loginErrorRedisKey).Int()
	if errCount >= 3 {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "密码输错3次禁止登入",
		}
		c.ServeJSON()
		return
	}
	var err error
	var user models.User
	err = models.DB.Table("users").Where("username = ?", username).First(&user).Debug().Error
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "用户查询失败",
		}
		c.ServeJSON()
		return
	}
	if user.ID == 0 {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "用户不存在",
		}
		c.ServeJSON()
		return
	}
	if user.Password != pkg.MD5str1(password) {
		cache.Rdb.Incr(cache.Ctx, loginErrorRedisKey)
		pwdCountErr, _ := cache.Rdb.Get(cache.Ctx, loginErrorRedisKey).Int()
		if pwdCountErr == 2 {
			cache.Rdb.Set(cache.Ctx, loginErrorRedisKey, 3, time.Hour*2)
			c.Data["json"] = map[string]interface{}{
				"code": 500,
				"msg":  "密码错误三次,封禁两个小时",
			}
			c.ServeJSON()
			return
		}
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "密码错误",
		}
		c.ServeJSON()
		return
	}
	cache.Rdb.Del(cache.Ctx, loginErrorRedisKey) //清空错误数据
	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"msg":  "登录成功",
	}
	c.ServeJSON()
	return
}
func (c *UserController) List() {
	fmt.Println("List")
	//1-从redis读取数据
	var user []models.User
	dataListKey := "data_id_"
	cacheJson, _ := cache.Rdb.Get(cache.Ctx, dataListKey).Result()
	if cacheJson == "" {
		models.DB.Model(&models.User{}).Find(&user)
		jsonData, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, dataListKey, jsonData, time.Hour*2)
	} else {
		err := json.Unmarshal([]byte(cacheJson), &user)
		if err != nil {
			c.Data["json"] = map[string]interface{}{
				"code": 500,
				"msg":  "缓存读取失败",
			}
			c.ServeJSON()
			return
		}
	}
}
func (c *UserController) Detail() {
	fmt.Println("Detail")
	id, _ := c.GetInt("id")
	dataListKey := "data_id_" + strconv.Itoa(id)
	cacheJson, _ := cache.Rdb.Get(cache.Ctx, dataListKey).Result()
	var user models.User
	if cacheJson == "" {
		models.DB.Model(&models.User{}).Where("id=?", id).First(&user)
		JsonData, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, dataListKey, JsonData, time.Hour*2)
	} else {
		err := json.Unmarshal([]byte(cacheJson), &user)
		if err != nil {
			c.Data["json"] = map[string]interface{}{
				"code": 500,
				"msg":  "缓存读取失败",
			}
			c.ServeJSON()
			return
		}
	}
	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"data": user,
		"msg":  "success",
	}
	c.ServeJSON()
	return
}
